package MyApp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyActivity extends AppCompatActivity {
    private static final String TAG = "MyActivity";
    private RecyclerView mRecyclerView;
    private MyListAdapter mAdapter;
    private ArrayList<HashMap<String, String>> mDataList;
    private Handler mainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mRecyclerView = findViewById(R.id.recycler_view);
        mAdapter = new MyListAdapter(this, R.layout.item_layout, new ArrayList<HashMap<String, String>>());
        mRecyclerView.setAdapter(mAdapter); // 为 RecyclerView 设置适配器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // 设置布局管理器

        mDataList = new ArrayList<>();
        mainHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 3) {
                    @SuppressWarnings("unchecked")
                    List<HashMap<String, String>> retlist = (List<HashMap<String, String>>) msg.obj;
                    mAdapter.addAll(retlist); // 使用 MyListAdapter 的方法更新数据
                }
                super.handleMessage(msg);
            }
        };

        // 启动线程获取数据
        MyThread listThread = new MyThread();
        listThread.setHandler(mainHandler);
        new Thread(listThread).start();
    }
}